package com.glsc.ngateway.flowable.listener.createproductnew;

import com.alibaba.fastjson.JSONObject;
import com.glsc.ngateway.flowable.service.FlowableUserService;
import com.glsc.ngateway.flowable.service.feign.IEurekaAmproductFeignService;
import org.flowable.engine.delegate.TaskListener;
import org.flowable.task.service.delegate.DelegateTask;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;

@Component
public class CreateProductNewConfirmHandler implements TaskListener {

    private static final Logger logger = LoggerFactory.getLogger(CreateProductNewConfirmHandler.class);

    public static CreateProductNewConfirmHandler createProductNewConfirmHandler;
    @Autowired
    private FlowableUserService flowableUserService;
    @Autowired
    IEurekaAmproductFeignService amproductFeignService;

    @PostConstruct
    public void init() {
        createProductNewConfirmHandler = this;
        createProductNewConfirmHandler.flowableUserService = this.flowableUserService;
        createProductNewConfirmHandler.amproductFeignService = this.amproductFeignService;
    }


    @Override
    public void notify(DelegateTask delegateTask) {

        try {

            //delegateTask.setAssignee("老板");
            String userid = delegateTask.getVariable("userid").toString();
            String content = delegateTask.getVariable("content").toString();


//        String department = createProductStartHandler.flowableUserService.getDepartment(userid);
//        delegateTask.setVariable("teamleader", createProductStartHandler.flowableUserService.getLeader(userid));
//        delegateTask.setVariable("manager", createProductStartHandler.flowableUserService.getManager(department));
//
            JSONObject json = JSONObject.parseObject(content);
            JSONObject js = json.getJSONObject("taskAssignee");
            String[] g = js.getString("otherPmGroup").split(",");
            List<String> group = new ArrayList<>();
            for (int i = 0; i < g.length; i++) {
                group.add(g[i]);
            }
            delegateTask.setVariable("otherPmGroup", group);
            delegateTask.setVariable("otherPmGroupSize", group.size());


        } catch (Exception e) {
            logger.error("createProductNewConfirmHandler处理异常，结束处理。processid=" + delegateTask.getProcessInstanceId(), e);
            throw new RuntimeException("createProductNewConfirmHandler处理异常，结束处理。processid=" + delegateTask.getProcessInstanceId(), e);
        }
    }

}